package control.report;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;

import org.apache.poi.hssf.usermodel.*;



public class JakartaExcelHandler extends ExcelHandler{
	private static HSSFWorkbook copy;
	private String path;
	private HSSFSheet sheet;
	private String newFilePath;
	private String secondpath;
	private List paths=new ArrayList();
	
	public void importFile(){ 
		String folder;
		if (this.isPrint()){
			folder="print";
		}
		else{
			folder="download";
		}
		File file = new File("C:/Documents and Settings/eneida/workspace/Protubo/Reports/orcamento/"+path+".xls");

//		File file = new File("/root/workspace/Protubo/Reports/orcamento/"+path+".xls");
//		newFilePath="/root/workspace/Protubo/Reports/orcamento/temp/"+path+"temp.xls";
		Integer i=0;

		String newpath="C:/Documents and Settings/eneida/workspace/Protubo/Reports/orcamento/"+folder+"/"+path+"-"+secondpath+"-"+i.toString()+".xls";		
		boolean exist=true;
		while (exist){
			File file2 = new File(newpath);
			exist=file2.exists();
			if (exist) {
				i++;
				newpath="C:/Documents and Settings/eneida/workspace/Protubo/Reports/orcamento/"+folder+"/"+path+"-"+secondpath+"-"+i.toString()+".xls";		
			}
		}
		newFilePath=newpath;
		try {

			this.copy = new HSSFWorkbook(new FileInputStream(file));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.sheet = copy.getSheet("Sheet1");
		// All cells modified/added. Now write out the workbook 
	}
	
	public void closeFile(){
		FileOutputStream out;
		try {
			out = new FileOutputStream(newFilePath);
			try {
				copy.write(out);
				paths.add(newFilePath);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void addString(int row, int column, String text){
		HSSFRow Row=sheet.getRow(row-1);
		HSSFCell cell=Row.getCell((short) (column-1));
		if (text!=null){
		cell.setCellValue(text);}
	}
	public void addNumber(int row, int column, Double number){
 
	}
	  public void Print_doc() {
		  try {
			Runtime.getRuntime().exec("Project1");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		  /*
	        View m_view = new View();
	        try
	        {
	            m_view.read("C:/Documents and Settings/eneida/workspace/Protubo/Reports/orcamento/"+path+".xls");
	            m_view.setPrintScale(100);     // set print scale value --- default is 100%
	            m_view.setPrintHeader("Your header");  //set header --- default is &A
	            m_view.setPrintFooter("Your footer");  //set footer  --- default is Page &P
	            m_view.setPrintGridLines(true);  //show grid line
	            m_view.write("C:/Documents and Settings/eneida/workspace/Protubo/Reports/orcamento/"+path+".pdf",View.eFilePdf);   //export to pdf file
	        }
	        catch (Exception e){
	        	
	        }
		  
	       DocFlavor format = DocFlavor.INPUT_STREAM.PDF;
	    //DocFlavor format = new DocFlavor("text/plain", "java.io.FileInputStream");
	       PrintRequestAttributeSet aset =new HashPrintRequestAttributeSet();
	 
	       aset.add(MediaSizeName.ISO_A4);
	       aset.add(new Copies(1));
	       //aset.add(Sides.ONE_SIDED);
	       //aset.add(Finishings.NONE);
	 
	       PrintService pservices=PrintServiceLookup.lookupDefaultPrintService();
	 
	       System.out.println(pservices.getName());
	       DocPrintJob djob=pservices.createPrintJob();
	 
	       try{
	       FileInputStream input =new FileInputStream("C:/Documents and Settings/eneida/workspace/Protubo/Reports/orcamento/"+path+".pdf");
	       Doc doc = new SimpleDoc(input,format,null);
	       djob.print(doc,aset);
	 
	       }
	       catch(FileNotFoundException exc){
	         exc.printStackTrace();
	         System.out.println("File not found");
	       }
	       catch(PrintException e){
	         e.printStackTrace();
	         System.out.println("Printer not found");
	         }*/
	  }

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public List getPaths() {
		return paths;
	}

	public void setPaths(List paths) {
		this.paths = paths;
	}

	public String getSecondpath() {
		return secondpath;
	}

	public void setSecondpath(String secondpath) {
		this.secondpath = secondpath;
	}


}
